package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 通用响应
// {
// code:0,
// succeed:false,
// message:"",
// content:""
// }

type Response struct {
	Code    int         `json:"code"`
	Succeed bool        `json:"succeed"`
	Message string      `json:"message"`
	Content interface{} `json:"content"`
}

type JsonOption func(*Response)

func WithCode(code int) JsonOption {
	return func(r *Response) {
		r.Code = code
	}
}

func WithSucceed(succeed bool) JsonOption {
	return func(r *Response) {
		r.Succeed = succeed
	}
}

func WithMessage(message string) JsonOption {
	return func(r *Response) {
		r.Message = message
	}
}
func WithContent(content interface{}) JsonOption {
	return func(r *Response) {
		r.Content = content
	}
}
func JSONOk(ctx *gin.Context, options ...JsonOption) {
	res := Response{
		Code:    0,
		Message: "请求成功",
		Succeed: true,
		Content: nil,
	}
	for _, option := range options {
		option(&res)
	}
	ctx.JSON(http.StatusOK, res)
}

func JSONErr(ctx *gin.Context, options ...JsonOption) {
	res := Response{
		Code:    0,
		Message: "请求异常",
		Succeed: false,
		Content: nil,
	}
	for _, option := range options {
		option(&res)
	}
	ctx.JSON(http.StatusOK, res)
}
